<template>
  <div class="payment-approval-page">
    <div class="search-wrapper">
      <super-search :search-form="searchForm" :search-item-config="searchItemList" @search="searchHandle"></super-search>
    </div>
    <div class="content-wrapper">
      <a-tabs v-model:activeKey="activeKey" @change="changeTabHandle">
        <a-tab-pane key="0" :tab="`待申请(${pageStat['0']})`"></a-tab-pane>
        <a-tab-pane key="1" :tab="`待审批(${pageStat['1']})`"></a-tab-pane>
        <a-tab-pane key="2" :tab="`审批通过(${pageStat['2']})`"></a-tab-pane>
        <a-tab-pane key="3" :tab="`审批驳回(${pageStat['3']})`"></a-tab-pane>
      </a-tabs>
      <super-table
        :height="tableHeight"
        ref="tableRef"
        :activeKey="activeKey"
        v-model:page="searchForm.pageNum"
        v-model:limit="searchForm.pageSize"
        :loading="loading"
        :data="tableData"
        :total="searchForm.total"
        :column="tableColumn"
        @change-page="getData"
        :sort-config="{ remote: true }"
        @sort-change="sortChangeHandle"
      >
        <template #tool>
          <template v-if="activeKey === '0'">
            <a-button type="primary" @click="applyPaymentHandle">发起付款申请</a-button>
            <a-button type="primary" v-debounce @click="downloadDataHandle">导出</a-button>
          </template>
          <template v-if="activeKey === '1'">
            <a-button type="primary" v-debounce @click="downloadDataHandle">导出</a-button>
            <a-button @click="batchPassApplyHandle" v-btnAuth="'PAYMENTAPPROVAL_BATCH_PASS_APPLY'">批量通过</a-button>
            <a-button @click="batchRefuseApplyHandle" v-btnAuth="'PAYMENTAPPROVAL_BATCHREFUSE_APPLY'">批量驳回</a-button>
          </template>
          <template v-if="activeKey === '2'">
            <a-button type="primary" v-debounce @click="downloadDataHandle">导出</a-button>
            <a-button @click="batchWriteOffFeeHandle" v-btnAuth="'PAYMENTAPPROVAL_BATCH_WRITE_OFFFEE'">批量核销费用</a-button>
          </template>
          <template v-if="activeKey === '3'">
            <a-button type="primary" v-debounce @click="downloadDataHandle">导出</a-button>
          </template>
          <a-button type="primary" @click="showStatictisHandle">查看统计</a-button>
        </template>
        <template #payableStatementNumber="{ row }">
          {{ row.payableStatementNumber }}
        </template>
        <template #applyNo="{ row }">
          <a-button type="link" @click="showApprovalInfoHandle(row)">{{ row.applyNo }}</a-button>
        </template>
        <template #verificaStatus="{ row }">
          <a-tag color="#f50" v-if="row.verificaStatus === 1">未核销</a-tag>
          <a-tag color="#87d068" v-if="row.verificaStatus === 2">已核销</a-tag>
        </template>
        <template #status="{ row }">
          <a-tag v-if="row.status === 1" color="#cccc"> 未生成账单 </a-tag>
          <a-tag v-if="row.state === 4" color="#2db7f5"> 部分核销 </a-tag>
          <a-tag v-if="row.state === 3" color="#f40"> 待核销 </a-tag>
          <a-tag v-if="row.status === 2" color="#108ee9"> 已生成账单 </a-tag>
          <a-tag v-if="row.status === 5" color="#87d068"> 已核销 </a-tag>
        </template>
        <template #action="{ row }">
          <template v-if="activeKey === '1'">
            <a-button type="link" @click="withDrawApplyHandle(row)">撤回申请</a-button>
            <a-button type="link" @click="passApplyHandle(row)" v-btnAuth="'PAYMENTAPPROVAL_PASSAPPLY'">通过申请</a-button>
            <a-button type="link" @click="refuseApplyHandle(row)" danger v-btnAuth="'PAYMENTAPPROVAL_REFUSE_APPLY'"> 驳回申请</a-button>
          </template>
          <template v-else-if="activeKey === '2'">
            <a-button type="link" @click="writeOffFeeHandle(row)" v-btnAuth="'PAYMENTAPPROVAL_WRITE_OFFFEE'">核销费用</a-button>
          </template>
          <template v-else-if="activeKey === '3'">
            <a-button type="link" @click="reSubmitApplyHandle(row)">重新提交</a-button>
            <a-button type="link" @click="withDrawApplyHandle(row)">撤回待申请</a-button>
          </template>
        </template>
      </super-table>
    </div>
    <!-- 抽屉 -->
    <a-drawer class="custom-drawer" v-model:visible="drawerOpts.visible" :title="drawerOpts.title" :maskClosable="false" :closable="true" @close="getData" :width="drawerOpts.width" placement="right">
      <component v-model:visible="drawerOpts.visible" :is="drawerOpts.component" :type="drawerOpts.type" :pass-data="drawerOpts.passData" @submit="drawerSubmitHandle"></component>
    </a-drawer>
    <!-- 弹窗 -->
    <a-modal
      v-model:visible="dialogOpts.visible"
      :maskClosable="false"
      :keyboard="false"
      :closable="['DataStatistics'].includes(dialogOpts.component)"
      :footer="null"
      :title="dialogOpts.title"
      :width="dialogOpts.width"
    >
      <component
        v-model:visible="dialogOpts.visible"
        :init="getStatisticsDataFn"
        :column="statisticsColumn"
        :is="dialogOpts.component"
        :type="dialogOpts.type"
        :pass-data="dialogOpts.passData"
        @submit="dialogSubmitHandle"
      ></component>
    </a-modal>
  </div>
</template>
<script lang="ts">
import Details from "./components/details/index.vue";
import ApplyPayment from "./components/applyPayment/index.vue";
import ApprovalPending from "./components/approvalPending/index.vue";
import WriteOffFee from "./components/writeOffFee/index.vue";
import RejectApply from "./components/rejectApply/index.vue";
import DataStatistics from "@/components/dataStatistics/index.vue";
import TableHook from "@/hook/tableHook";
export default {
  name: "PaymentApproval",
  components: {
    Details,
    ApplyPayment,
    ApprovalPending,
    WriteOffFee,
    RejectApply,
    DataStatistics
  }
};
</script>
<script lang="ts" setup>
import { searchItemList } from "./data";
import TableHeightHook from "@/hook/tableHeightHook";
import ModalHook from "@/hook/modalHook";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
const { tableHeight, loading } = TableHeightHook(110);
const { statisticsColumn, searchForm, tableData, tableRef, dialogOpts, drawerOpts, activeKey, pageStat, tableColumn } = InitHook();
const {
  searchHandle,
  getData,
  changeTabHandle,
  batchWriteOffFeeHandle,
  showApprovalInfoHandle,
  applyPaymentHandle,
  downloadDataHandle,
  writeOffFeeHandle,
  reSubmitApplyHandle,
  withDrawApplyHandle,
  passApplyHandle,
  refuseApplyHandle,
  batchPassApplyHandle,
  batchRefuseApplyHandle,
  getStatisticsDataFn,
  showStatictisHandle
} = OperationHook({
  activeKey,
  pageStat,
  searchForm,
  tableData,
  tableRef,
  dialogOpts,
  drawerOpts,
  loading
});
const { sortChangeHandle } = TableHook({ searchForm }, getData);
const { drawerSubmitHandle, dialogSubmitHandle } = ModalHook({
  drawerOpts,
  dialogOpts,
  callBack: () => searchHandle()
});
searchHandle();
</script>
<style lang="stylus" scoped></style>
